<template>
  <!--查杀毒记录-->
  <div  v-loading.fullscreen.lock="fullscreenLoading"></div>
  <basic-dialog v-model="dialog.showDialog" :title="dialog.title" width="1500"
                :autoClose="false" :hideFooter="true">
    <basic-crud  ref="crudRef" :option="options" :autoLoad="false"
                 :fastSearch="{}">
      <template #menu-right>
        <div style="display: flex;align-items: center;margin-right: 10px;">
          <span style="width: 10px;text-align: right"></span>
          <el-input v-model="fastSearchText" clearable :placeholder="'请输入单据号、产品名称、产品型号、产品编号回车查询'" size="small"
                    style="margin-right: 5px;width: 500px" @clear="fastSearchHandler"
                    @keyup.enter.native="fastSearchHandler">
            <template #suffix>
              <el-icon class="el-input__icon" style="cursor: pointer" @click="fastSearchHandler"><search /></el-icon>
            </template>
          </el-input>
        </div>
      </template>
      <template #menu-left>
        <el-button v-if="!onlyShow"
                   type="primary"
                   icon="el-icon-plus"
                   @click="openFormHandler('add', workRow.id)"
        >新增
        </el-button>
      </template>
      <template #menu="{ row, size }">
        <el-button
            type="text"
            icon="el-icon-view"
            @click="handleView(row)"
        >查看
        </el-button>
        <el-button v-if="!onlyShow"
                   type="text"
                   icon="el-icon-edit"
                   @click="openFormHandler('edit', workRow.id, row)"
        >编辑
        </el-button>
        <el-button v-if="!onlyShow"
                   class="bg-delete"
                   type="text"
                   icon="el-icon-edit"
                   @click="handleDelete(row)"
        >删除
        </el-button>
      </template>
    </basic-crud>
  </basic-dialog>
  <antivirus-records-form
      ref="antivirusRecordsFormRefs"
      @refresh="refreshHandler"
  ></antivirus-records-form>
</template>

<script setup>
import {defineExpose, ref,nextTick} from "vue";
import {ElMessage} from "element-plus";
import { apiGetList, deleteDel } from "@/apis/mes/workAntivirusRecord.js";
import { getWorkOrder } from "@/apis/mes/processTask";
import { formatDate } from "@/utils/formatTime";
import AntivirusRecordsForm from "@/views/mes/techne/AntivirusRecordsForm.vue";

const emits = defineEmits(['refresh-parent'])

const dialog = ref({
  showDialog: false,
  title: "查杀毒记录"
})
const formDialog = ref({
  showDialog: false,
  title: "新增"
})
const workRow = ref({});
const onlyShow = ref(false);
const searchData=ref({})

// 打开新增编辑对话框
const openDialog = (row, onlyShowValue) => {
  workRow.value=row;
  onlyShow.value=onlyShowValue
  options.value.menuWidth= !onlyShow.value?180:80
  searchData.value={
    processTaskId: workRow.value.id
  }
  getTableList()
};

const crudRef=ref(null);
const options= ref({
  addBtn: false,
  editBtn: false,
  delBtn: false,
  refreshBtn: false,
  viewBtn: false,
  align: "center",
  height: "570px",
  menu: true,
  menuWidth: 180,
  column: [{
    prop: 'billCode',
    label: '单据号',
    minWidth:200,
    display: false,
    sortable: true,
  }, {
    prop: "productName",
    label: "产品名称",
    minWidth:160,
    display: false,
    sortable: true,
  }, {
    prop: "modelName",
    label: "产品型号",
    display: false,
    sortable: true
  },{
      prop: "productCode",
      label: "产品编号",
      display: false,
      sortable: true
    },{
      prop: "source",
      label: "来源",
      display: false,
    },{
      prop: "sourceTime",
      label: "来源时间",
      width: 100,
      display: false,
      formatter: (row) => {
        return formatDate(row.sourceTime, "yyyy-MM-dd");
      },
    },{
      prop: "antivirusOpportunity",
      label: "查杀病毒时机",
      width: 200,
      display: false,
    }]
})

const fullscreenLoading = ref(false)
const doFirst = ref(false);
const getTableList=()=> {
  fullscreenLoading.value = true
  apiGetList(searchData.value,fullscreenLoading).then((res) => {
    if (res.success) {
      if (!onlyShow.value && res.total == 0 && !doFirst.value) {
        doFirst.value = true;
        nextTick(() => {
          openFormHandler('add', workRow.value.id);
          dialog.value.showDialog = false;
        });
      }else {
        doFirst.value = true;
        dialog.value.showDialog = true;
        nextTick(() => {
          crudRef.value.tableData = res.data;
          crudRef.value.pageData.total = res.total;
        });
      }
    } else {
      ElMessage.error(res.msg);
    }
  }).catch(() => {ElMessage.error("获取列表数据失败!")});
}

const fastSearchText=ref('')
//多属性快速查询
const fastSearchHandler=()=> {
  if(fastSearchText.value){
    searchData.value={
      processTaskId: workRow.value.id,
      fastColumn:'billCode,productName,modelName,productCode',
      fastText:fastSearchText.value
    }
  }else{
    searchData.value={
      processTaskId: workRow.value.id
    }
  }
  getTableList();
}

const antivirusRecordsFormRefs=ref(null);
const openFormHandler=(type, workId, row)=> {
  //先从后台获取下工单的信息，用于显示产品等几个属性
  getWorkOrder(workId).then((obj) => {
    if (
        !obj.pdmProductCode ||
        obj.pdmProductCode == null ||
        obj.pdmProductCode == ""
    ) {
      return ElMessage.error("任务的产品编号都还没有添加，请先执行开工");
    }
    const params = {
      workOrder: obj,
      type: type,
      processTaskId: workRow.value.id,
      row: type === "edit" ? row : {},
    };
    antivirusRecordsFormRefs.value?.openDialog(params);
  });
}

const handleView=(row) =>{
  const params = {
    type: "view",
    row: row,
  };
  antivirusRecordsFormRefs.value?.openDialog(params);
}

// 删除
const handleDelete=(row)=> {
  ElMessageBox.confirm(`此操作将永久删除数据, 是否继续?`, "提示").then(() => {
    deleteDel(row.id).then((res) => {
      ElMessage.success("删除成功");
      doFirst.value = true;
      getTableList();
      emits('refresh-parent')
    });
  })
}

const refreshHandler=()=> {
  getTableList()
  emits('refresh-parent')
}

defineExpose({
  openDialog
})
</script>

<style scoped>

</style>